//
//  YJLoginPageViewController.m
//  Sports-Go
//
//  Created by rimi on 16/9/22.
//  Copyright © 2016年 YRL. All rights reserved.
//

#import "YJLoginPageViewController.h"
#import "YJCateAnimation.h"
#import "YJRegisterViewController.h"
#import "YJFindThePassWordViewController.h"
#import "RZAnimationIndicator.h"
#import <ShareSDK/ShareSDK.h>
@interface YJLoginPageViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UIButton *loginButton;
@property (nonatomic,strong) UIButton *registerButton;
@property (nonatomic,strong) UITextField *userTextField;
@property (nonatomic,strong) UITextField *passwordField;
@property (nonatomic,strong) UIButton *forgetThePasswordButton;
@property (nonatomic,strong) YJCateAnimation *cateAnimation;
@property (nonatomic,strong) UILabel *userLabel;
@property (nonatomic,strong) UILabel *passLabel;
@property (assign,nonatomic) ClickType clicktype;
@property (nonatomic, strong) RZAnimationIndicator *animationIndicator;
@end

@implementation YJLoginPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:RzCGRectMake(0, 0, 375, 667)];
    
    backImage.image = [UIImage imageNamed:@"loginback"];
    [self.view addSubview:backImage];

    UIButton *backButton = [[UIButton alloc] initWithFrame:RzCGRectMake(30, 50, 50, 30)];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backtheLastPage) forControlEvents:UIControlEventTouchUpInside];
    _cateAnimation = [[YJCateAnimation alloc] init];
    _cateAnimation.frame = RzCGRectMake(0, 50, 375, 400);
    
    UIView *xian = [[UIView alloc] initWithFrame:RzCGRectMake(0, 542, 150, 1)];
    xian.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:RzCGRectMake(150, 535, 75, 15)];
    label.text = @"第三方登录";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:12];
    
    UIView *xian1 = [[UIView alloc] initWithFrame:RzCGRectMake(225, 542, 150, 1)];
    xian1.backgroundColor = [UIColor whiteColor];
    
  //第三方qq登录
    UIButton *qqButton = [[UIButton alloc] initWithFrame:RzCGRectMake(120, 565, 40, 40)];
    [qqButton setImage:[UIImage imageNamed:@"qq1"] forState:UIControlStateNormal];
    [qqButton addTarget:self action:@selector(useqqLogin) forControlEvents:UIControlEventTouchUpInside];
   //第三方微博登录
    UIButton *weiboButton = [[UIButton alloc] initWithFrame:RzCGRectMake(220, 565, 40, 40)];
    [weiboButton setImage:[UIImage imageNamed:@"微博1"] forState:UIControlStateNormal];
    [weiboButton addTarget:self action:@selector(useTheWeiBoLogin) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *maskView = [[UIView alloc] initWithFrame:RzCGRectMake(30, 250, 315, 110)];
    maskView.backgroundColor = getColor(58, 206, 248, 1);
    //用户名label
    _userLabel = [[UILabel alloc] initWithFrame:RzCGRectMake(5, 10, 70, 40)];
    _userLabel .text = @"用户名:";
    _userLabel.textColor = [UIColor whiteColor];
    //密码label
    _passLabel = [[UILabel alloc] initWithFrame:RzCGRectMake(5, 55, 70, 40)];
    _passLabel.text = @"密   码:";
    _passLabel.textColor = [UIColor whiteColor];
    
    [self.view addSubview:qqButton];
    [self.view addSubview:weiboButton];
    [self.view addSubview:_cateAnimation];
    
    [self.view addSubview:xian];
    [self.view addSubview:xian1];
    [self.view addSubview:label];
    [self.view addSubview:maskView];
    [self.view addSubview:backButton];
    
    [maskView addSubview:_userLabel];
    [maskView addSubview:_passLabel];

    [maskView addSubview:self.userTextField];
    
    [maskView addSubview:self.passwordField];
    
    [self.view addSubview:self.loginButton];
    [self.view addSubview:self.registerButton];
    [self.view addSubview:self.forgetThePasswordButton];
    
    
}

#pragma mark -- button click
- (void)backtheLastPage{

    [self.navigationController popViewControllerAnimated:YES];
    
}
//登录
- (void)enterTheLoginPage:(UIButton *)sender{
  
    if ([self.userTextField.text isEqualToString:@""] || [self.passwordField.text isEqualToString:@""]) {
        if ([self.userTextField.text isEqualToString:@""]) {
            [self warnWitherrorInfo:@"用户名不可为空"];
            [self.userTextField becomeFirstResponder];
        }else{
            [self warnWitherrorInfo:@"密码不可为空"];
            [self.passwordField becomeFirstResponder];
        }
    }else{
        
        [AVUser logInWithUsernameInBackground:self.userTextField.text password:self.passwordField.text block:^(AVUser *user, NSError *error) {


            if (error) {
                
                [self warnWitherrorInfo:@"登录错误"];
                
            }else{
                
                if (user != nil) {
                    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
                    [[NSUserDefaults standardUserDefaults] setObject:self.userTextField.text forKey:@"userName"];
                    [self backtheLastPage];
            
                } else {
                    [self warnWitherrorInfo:@"用户名不存在"];
                }
            }
            
        }];
        }
        
}

//注册
- (void)enterTheRegisterPage:(UIButton *)sender{
    self.passwordField.text = @"";
    self.userTextField.text = @"";
    [self.userTextField becomeFirstResponder];
    YJRegisterViewController *registerVC = [[YJRegisterViewController alloc] init];
    
    [self.navigationController pushViewController:registerVC animated:YES];
   
}
//找回密码
- (void)enterTheGetThePasswordPage:(UIButton *)sender{

    YJFindThePassWordViewController *findThePassWordVC = [[YJFindThePassWordViewController alloc] init];
    [self.navigationController pushViewController:findThePassWordVC animated:YES];
}

#pragma mark -- the third qq Login
//第三方qq登录
- (void)useqqLogin{
    [self.animationIndicator startAnimation];
[ShareSDK getUserInfo:SSDKPlatformTypeQQ onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
    [self.animationIndicator stopAnimationWithLoadText:@"完成" withType:YES];
    if (state == SSDKResponseStateSuccess)
    {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:user.nickname forKey:@"nickname"];
        NSInteger gender= user.gender;
        if (gender == 0) {
            //男
            [dic setValue:@"男" forKey:@"gender"];

        }if (gender == 1) {
            //女
            [dic setValue:@"女" forKey:@"gender"];
        } else {
            //未知
            [dic setValue:@"未知" forKey:@"gender"];
        }
       
            //qq平台
        [dic setValue:@"998" forKey:@"platformType"];
        [dic setValue:user.rawData[@"figureurl_qq_2"] forKey:@"icon"];
        [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"userInfo"];
        [[NSUserDefaults standardUserDefaults] setObject:user.nickname forKey:@"userName"];
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
        
        [self backtheLastPage];
    }
    else
    {
        NSLog(@"%@",error);
    }
}];
    
}
//第三方微博登录
- (void)useTheWeiBoLogin{
    [ShareSDK getUserInfo:SSDKPlatformTypeSinaWeibo onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        if (state == SSDKResponseStateSuccess)
        {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            //新浪微博平台
            
            [dic setValue:@"1" forKey:@"platformType"];
            [dic setValue:user.nickname forKey:@"nickname"];
            NSInteger gender= user.gender;
            if (gender == 0) {
                //男
                [dic setValue:@"男" forKey:@"gender"];
                
            }if (gender == 1) {
                //女
                [dic setValue:@"女" forKey:@"gender"];
            } else {
                //未知
                [dic setValue:@"未知" forKey:@"gender"];
            }
            
            [dic setValue:user.rawData[@"avatar_hd"] forKey:@"icon"];
            [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"userInfo"];
            
           [[NSUserDefaults standardUserDefaults] setObject:user.nickname forKey:@"userName"];
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
            
            [self backtheLastPage];
            
        }
        
        else
        {
            NSLog(@"%@",error);
        }

    }];
}
#pragma mark -- alter
- (void)warnWitherrorInfo:(NSString *)error{
    //警示框
    UILabel *label = [[UILabel alloc]initWithFrame:RzCGRectMake(100, 270, 185, 50)];
    label.backgroundColor = [UIColor lightGrayColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    label.text = error;
    label.layer.cornerRadius = 5;
    label.layer.masksToBounds = YES;
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    [UIView animateWithDuration:2.0 animations:^{
        label.alpha = 0.0;
    } completion:^(BOOL finished) {
        
        [label removeFromSuperview];
    }];
    
    
}

#pragma mark -- uitextfiled   delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField.tag == 500) {
        //账号
        
        if (_clicktype != clicktypePass) {
            _clicktype = clicktypeUser;
            return;
        }
        _clicktype = clicktypeUser;
        [_cateAnimation openTheEyes];//不蒙眼
        
    }else{
        
     //密码
        if (_clicktype == clicktypePass)
        {
            return;
        }
        _clicktype = clicktypePass;
        [_cateAnimation closeTheEyes];//动画 蒙眼

    }
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if ([textField isEqual:_userTextField]) {
        [_passwordField becomeFirstResponder];
    }else{
    
      [textField resignFirstResponder];
    }
    
    
    return YES;
}

#pragma mark -- lazy
- (UITextField *)userTextField{

    if (!_userTextField) {
        _userTextField = [[UITextField alloc] initWithFrame:RzCGRectMake(75, 10, 230, 40)];
        _userTextField.placeholder = @"请输入用户名";
        _userTextField.backgroundColor = [UIColor whiteColor];
        _userTextField.layer.cornerRadius = 5;
        _userTextField.layer.masksToBounds = YES;
        _userTextField.delegate = self;
        _userTextField.tag = 500;
        _userTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        
    }
    return _userTextField;
}

- (UITextField *)passwordField{

    if (!_passwordField) {
        _passwordField = [[UITextField alloc] initWithFrame:RzCGRectMake(75, 60, 230, 40)];
        _passwordField.placeholder = @"请输入登录密码";
        _passwordField.backgroundColor = [UIColor whiteColor];
        _passwordField.layer.cornerRadius = 5;
        _passwordField.layer.masksToBounds = YES;
        _passwordField.delegate = self;
        _passwordField.tag = 501;
        _passwordField.secureTextEntry = YES;
         _passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _passwordField;
}

- (UIButton *)loginButton{

    if (!_loginButton) {
        _loginButton = [[UIButton alloc] initWithFrame:RzCGRectMake(375/3.0, 375, 375/3.0, 40)];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
       
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _loginButton.backgroundColor = getColor(121, 185, 150, 1);
        _loginButton.layer.cornerRadius = 5;
        _loginButton.layer.masksToBounds = YES;
        [_loginButton addTarget:self action:@selector(enterTheLoginPage:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}
- (UIButton *)registerButton{
    if (!_registerButton) {
        _registerButton = [[UIButton alloc] initWithFrame:RzCGRectMake(375/3.0, 425, 375/3.0, 40)];
        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    
        [_registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _registerButton.backgroundColor = getColor(193, 162, 151, 1);
        _registerButton.layer.cornerRadius = 5;
        _registerButton.layer.masksToBounds = YES;
        [_registerButton addTarget:self action:@selector(enterTheRegisterPage:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerButton;
}
- (UIButton *)forgetThePasswordButton{
    if (!_forgetThePasswordButton) {
        _forgetThePasswordButton = [[UIButton alloc] initWithFrame:RzCGRectMake(150, 485, 75, 35)];
        _forgetThePasswordButton.backgroundColor = [UIColor clearColor];
        [_forgetThePasswordButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
        [_forgetThePasswordButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _forgetThePasswordButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_forgetThePasswordButton addTarget:self action:@selector(enterTheGetThePasswordPage:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetThePasswordButton;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    self.tabBarController.tabBar.hidden = YES;
    [self.view endEditing:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    self.tabBarController.tabBar.hidden = NO;
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
